package com.bibrApp.difu.fragment.home_mvp;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bibrApp.difu.DfApp;
import com.bibrApp.difu.R;
import com.bibrApp.difu.Response.BannerResBody;
import com.bibrApp.difu.Response.HomeMarketItemBean;
import com.bibrApp.difu.Response.NoticeResBody;
import com.bibrApp.difu.actvity.beginner_guide_mvp.BeginnerGuideActivity;
import com.bibrApp.difu.actvity.connect_mvp.ConnectActivity;
import com.bibrApp.difu.actvity.df_u_mvp.UActivity;
import com.bibrApp.difu.actvity.login_mvp.LoginActivity;
import com.bibrApp.difu.actvity.main_mvp.MainActivity;
import com.bibrApp.difu.actvity.my_mvp.MyActivity;
import com.bibrApp.difu.actvity.notice_detail_mvp.NoticeDetailActivity;
import com.bibrApp.difu.actvity.others.HelpActivity;
import com.bibrApp.difu.actvity.others.TestActivity;
import com.bibrApp.difu.actvity.others.WebActivity;
import com.bibrApp.difu.actvity.u_account_mvp.UAccountActivity;
import com.bibrApp.difu.adapter.MarketAdapter;
import com.bibrApp.difu.base.BaseFragment;
import com.bibrApp.difu.base.BasePermissionActivity;
import com.bibrApp.difu.utils.IntentUtil;
import com.bibrApp.difu.widget.Banner;
import com.bibrApp.difu.widget.HorizontalDividerItemDecoration;
import com.bibrApp.difu.widget.NoticeView;
import com.google.android.material.tabs.TabLayout;
import com.uuzuche.lib_zxing.activity.CaptureActivity;


import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import butterknife.BindView;
import butterknife.OnClick;
import io.socket.client.Socket;

public class HomeFragment extends BaseFragment<HomePresenter> implements IHomeView, TabLayout.OnTabSelectedListener {

    private Context mContext;
    private FragmentActivity mActivity;
    @BindView(R.id.home_tab_layout)
    TabLayout tabLayout;
    @BindView(R.id.home_notice_view)
    NoticeView noticeView;
    @BindView(R.id.home_banner)
    Banner homeBanner;
    @BindView(R.id.ll_dot)
    LinearLayout dotLL;
    @BindView(R.id.home_recycler_view)
    RecyclerView recyclerView;
    private MarketAdapter marketAdapter;
    private static final String[] tabs = {"OKEx", "火币", "币安"};
    private static final String[] tabsType = {"OKEX", "HUOBI", "BINANCE"};
    private Socket mSocket;

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initPresenter() {
        presenter = new HomePresenter(getActivity(), this);
        presenter.init();
    }

    @Override
    public void error(String msg) {

    }

    @Override
    public void successOfNotice(NoticeResBody data) {
        List<String> notices = new ArrayList<>();
        for (NoticeResBody.ListBean bean : data.getList()) {
            notices.add(bean.getTitle());
        }

        noticeView.addNotice(notices);
        noticeView.startFlipping();
        noticeView.setOnNoticeClickListener((position, notice) -> {
            String id = data.getList().get(position).getId();
            Intent intent = new Intent(mActivity, NoticeDetailActivity.class);
            intent.putExtra("id", id);
            startActivity(intent);
        });
    }

    @Override
    public void successOfBanner(BannerResBody data) {
        ArrayList<String> urls = new ArrayList<>();
        for (BannerResBody.ListBean bean : data.getList()) {
            Log.d("dj", "=url==" + bean.getImageUrl());
            urls.add(bean.getImageUrl());
        }
        homeBanner.start(mContext, urls, null, 3000, dotLL, R.mipmap.banner_dot_on, R.mipmap.banner_dot_off);
        homeBanner.setOnBannerItemClickListener(curIndex -> {
            //banner
            BannerResBody.ListBean bean = data.getList().get(curIndex);
            int type = bean.getStatus();
            /*Intent intent = new Intent();
            switch (type) {
                case 1:
                    intent.setClass(mActivity, NoticeDetailActivity.class);
                    intent.putExtra("id", bean.getAnnoId());

                case 0:
                default:
                    intent.setClass(mActivity, WebViewTurnTableActivity.class);
                    intent.putExtra("url", bean.getPointingUrl());
                    intent.putExtra("title", bean.getTitle());
                    break;
            }
            startActivity(intent);*/
        });
    }

    private List<HomeMarketItemBean> mData = new ArrayList<>();

    @Override
    public void successOfMarket(List<HomeMarketItemBean> homeMarketItemBeanList) {
        mActivity.runOnUiThread(() -> {
            mData.clear();
            if (homeMarketItemBeanList != null && homeMarketItemBeanList.size() > 0) {
                mData.addAll(homeMarketItemBeanList);
            }
            marketAdapter.setNewData(mData);
        });
    }

    private DfApp dfApp;
    @SuppressLint("WrongConstant")
    @Override
    public void initView() {
        mContext = getContext();
        mActivity = getActivity();

        dfApp = (DfApp) getActivity().getApplication();
        mSocket = dfApp.getSocket();
        dfApp.socketOnEvent(mSocket);
        presenter.getRecommandSocket(mSocket, tabsType[0]);
        presenter.getNoticeData();
        presenter.getBanner();

        tabLayout.addTab(tabLayout.newTab().setCustomView(getTabView(0)));
        tabLayout.addTab(tabLayout.newTab().setCustomView(getTabView(1)));
        tabLayout.addTab(tabLayout.newTab().setCustomView(getTabView(2)));
        tabLayout.addOnTabSelectedListener(this);
        updateTabTextView(tabLayout.getTabAt(tabLayout.getSelectedTabPosition()), true);

        marketAdapter = new MarketAdapter(mContext);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setItemAnimator(null);
        recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(mContext)
                .marginResId(R.dimen.dp_12, R.dimen.dp_12)
                .color(getResources().getColor(R.color.black_f0))
                .build());
        recyclerView.setAdapter(marketAdapter);
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();

    }

    @OnClick(R.id.home_cloud)
    public void helpClick() {
        startActivity(HelpActivity.class);
    }

    private final String URL = "https://wallet.trezor.io";
    @OnClick(R.id.home_treasure)
    public void uClick() {
        IntentUtil.toActivity(TestActivity.class, this);
    }

    @OnClick(R.id.home_z_xing)
    public void clickZxing() {
        ((BasePermissionActivity) getActivity()).reqPermission(new String[]{Manifest.permission.CAMERA}, () -> IntentUtil.toActivityForResult(CaptureActivity.class, HomeFragment.this, MainActivity.REQUEST_CODE));
    }

    @OnClick(R.id.login_tv)
    public void goToLogin() {
        IntentUtil.toActivity(HelpActivity.class, this);
    }

    @OnClick(R.id.my_tv)
    public void gotoMy() {
        if(dfApp.getmData() !=null){
            IntentUtil.toActivity(MyActivity.class, this);
        }else {
            IntentUtil.toActivity(LoginActivity.class, this);
        }

    }

    @OnClick(R.id.home_beginner_guide)
    public void clickBeginnerGuide(){
        IntentUtil.toActivity(BeginnerGuideActivity.class,this);
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        presenter.getRecommandSocket(mSocket, tabsType[tab.getPosition()]);
        updateTabTextView(tab, true);
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {
        updateTabTextView(tab, false);
    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }

    private View getTabView(int currentPosition) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.home_tab_item, null);
        TextView textView = view.findViewById(R.id.tab_item_textview);
        textView.setText(tabs[currentPosition]);
        return view;
    }

    private void updateTabTextView(TabLayout.Tab tab, boolean isSelect) {
        if (isSelect) {
            TextView tabSelect = tab.getCustomView().findViewById(R.id.tab_item_textview);
            tabSelect.setTextAppearance(mContext, R.style.TabLayoutTextStyle);
            tabSelect.setTextColor(mContext.getResources().getColor(R.color.blue_e4));
        } else {
            TextView tabUnSelect = tab.getCustomView().findViewById(R.id.tab_item_textview);
            tabUnSelect.setTextAppearance(mContext, R.style.TabLayoutUnTextStyle);
            tabUnSelect.setTextColor(mContext.getResources().getColor(R.color.black_a7));
        }
    }
}
